#include "StateFactory.h"

#include "StringUtility.h"

StateFactory::StateFactory() : GameBaseState()
{
    // Constructor, set up static game variables here
    testText.loadFont("fonts/unispace.ttf",24);
}

StateFactory::~StateFactory()
{
    // Destructor, deallocate pointers here
}

void StateFactory::init()
{
    // init function, set up variable game vars here (such as timers)
    // This will be called right before the state is activated (the constructor might be called before that)
    timer.start(3000);
}


void StateFactory::onPause()
{
    // Prepare game for being paused
    // Pause all running timers for example
    timer.pause();
}

void StateFactory::onResume()
{
    // Wake up game state from being paused
    // Resume all paused timers for example
    timer.unpause();
}

void StateFactory::userInput()
{
    // Handle all user input
    // Use SimpleJoy* BaseState::input variable
    // for example:
    // if (input->isStart()) pauseToggle();
    input->update();

    // This is triggered when the user presses the close button (X) on a PC and ensures it actually closes the window
#ifdef PLATFORM_PC
    if(input->isQuit())
    {
        nullifyState();
        return;
    }
#endif

    // you should always keep this check in here to enable the pause menu
    if (input->isStart())
    {
        pauseToggle();
        return;
    }

    if (input->isA())
    {
        timer.start();
        score += 100;
    }
    else if (input->isB())
    {
        if (not checkLives(--lives))
            loseGame();
    }

    input->resetKeys();
}

void StateFactory::update()
{
    // Update the game
    // This is called periodically before render()
    timer.update();

    if (timer.hasFinished())
        winGame();      // this marks the current level as beaten and skips to the next one
}

void StateFactory::render()
{
    // Render the game to the screen
    // Is called after update()
    GFX::clearScreen();     // this will clear the whole screen and should always be called first

    testText.setPosition(50,50);
    testText.print(StringUtility::intToString(timer.getTimeLeft()));
    testText.setPosition(50,200);
    testText.print("Lives: " + StringUtility::intToString(lives));
}
